package com.hongfasoft.util;

import com.hongfasoft.wx.service.AccessToken;

public class TokenAccessManager implements Runnable
{
	private static AccessToken token = null;
	private static Object object = new Object();
	private static boolean isRunning = false;

	public static AccessToken getAccessToken()
	{
		synchronized (object)
		{
			if (token == null || !token.isAlive())
			{
				System.out.println("refresh Token");
				token = WeixinUtil.getAccessToken();
				System.out.println(token.toString());
			}
		}

		return token;
	}

	public synchronized void startup()
	{
		synchronized (object)
		{
			if (!isRunning)
			{
				isRunning = true;
				Thread t = new Thread(this);
				t.start();
			}
		}
	}

	@Override
	public void run()
	{
		// TODO Auto-generated method stub
		while (isRunning)
		{
			try
			{
				getAccessToken();
				Thread.sleep(1000);
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
